<?php
/*
 * @Author: lokei
 * @Date: 2022-08-08 13:07:43
 * @LastEditors: lokei
 * @LastEditTime: 2022-11-19 01:13:16
 * @Description: 
 */

namespace App\Http\Controllers\Platform\Conf;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Platform\Conf\ConfAlipayModel;
use Illuminate\Http\Request;

class AlipayController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    //

    function resData($origin) {
        $data = array();
        $data['merchantPrivateKey'] = $origin->merchantPrivateKey;
        $data['alipayCertPath'] = $origin->alipayCertPath;
        $data['alipayRootCertPath'] = $origin->alipayRootCertPath;
        $data['merchantCertPath'] = $origin->merchantCertPath;
        $data['secret_key'] = $origin->secret_key;
        return $data;
    }

    public function get() {
        $conf = ConfAlipayModel::first();
        if(is_null($conf)) {
            $conf = new ConfAlipayModel();
            $conf->save();
            $conf = ConfAlipayModel::first();
        }
        $res = ResultTool::success();
        $res['data'] = !is_null($conf) ? $this->resData($conf) : null;
        return $res;
    }

    public function update(Request $request) {
        $conf = ConfAlipayModel::first();
        if(is_null($conf)) {
            $conf = new ConfAlipayModel();
        }
        $conf->merchantPrivateKey = $request->input('merchantPrivateKey');
        $conf->alipayCertPath = $request->input('alipayCertPath');
        $conf->alipayRootCertPath = $request->input('alipayRootCertPath');
        $conf->merchantCertPath = $request->input('merchantCertPath');
        $conf->secret_key = $request->input('secret_key');
        $conf->save();
        return ResultTool::success();
    }
    
}
